package org.jeecg.modules.demo.zhidingzhouqigongzuo.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.YZhidingzhouqigongzuo;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.YPeihebumenbiao;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.mapper.YPeihebumenbiaoMapper;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.mapper.YZhidingzhouqigongzuoMapper;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.service.IYZhidingzhouqigongzuoService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 制定周期性工作
 * @Author: jeecg-boot
 * @Date:   2025-10-27
 * @Version: V1.0
 */
@Service
public class YZhidingzhouqigongzuoServiceImpl extends ServiceImpl<YZhidingzhouqigongzuoMapper, YZhidingzhouqigongzuo> implements IYZhidingzhouqigongzuoService {

	@Autowired
	private YZhidingzhouqigongzuoMapper yZhidingzhouqigongzuoMapper;
	@Autowired
	private YPeihebumenbiaoMapper yPeihebumenbiaoMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(YZhidingzhouqigongzuo yZhidingzhouqigongzuo, List<YPeihebumenbiao> yPeihebumenbiaoList) {
		yZhidingzhouqigongzuoMapper.insert(yZhidingzhouqigongzuo);
		if(yPeihebumenbiaoList!=null && yPeihebumenbiaoList.size()>0) {
			for(YPeihebumenbiao entity:yPeihebumenbiaoList) {
				//外键设置
				entity.setZhidingbiaoid(yZhidingzhouqigongzuo.getId());
				yPeihebumenbiaoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(YZhidingzhouqigongzuo yZhidingzhouqigongzuo,List<YPeihebumenbiao> yPeihebumenbiaoList) {
		yZhidingzhouqigongzuoMapper.updateById(yZhidingzhouqigongzuo);
		
		//1.先删除子表数据
		yPeihebumenbiaoMapper.deleteByMainId(yZhidingzhouqigongzuo.getId());
		
		//2.子表数据重新插入
		if(yPeihebumenbiaoList!=null && yPeihebumenbiaoList.size()>0) {
			for(YPeihebumenbiao entity:yPeihebumenbiaoList) {
				//外键设置
				entity.setZhidingbiaoid(yZhidingzhouqigongzuo.getId());
				yPeihebumenbiaoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		yPeihebumenbiaoMapper.deleteByMainId(id);
		yZhidingzhouqigongzuoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			yPeihebumenbiaoMapper.deleteByMainId(id.toString());
			yZhidingzhouqigongzuoMapper.deleteById(id);
		}
	}

	//获取部门id
	public String getDepartId() {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departId = sysUser.getDepartIds();
		return departId;
	}
	//获取责任主管
	@Override
	public List<SysUser> getZerenzhuguanid() {
		//获取部门id
		String departId = getDepartId();
		return yZhidingzhouqigongzuoMapper.queryUserList(departId);
	}

	@Override
	public List<SysDepart> getPeihebumen(){
		return yZhidingzhouqigongzuoMapper.queryDepartList();
	}
	
}
